package com.example.block_chain.utils.Admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.block_chain.domain.pojo.Admin;
import com.example.block_chain.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class AdminDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a_no", username);
        Admin admin = adminMapper.selectOne(queryWrapper);

        if (admin == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        return new AdminDetailsImpl(admin);
    }
}
